package youleche.com.controler.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BEmailModule;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MEmailModule;
import youleche.com.util.DateUtil;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SEmailModuleServletAuthority
 */
public class SEmailModuleServletAuthority extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SEmailModuleServletAuthority() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//根据邮件模板名称获取模板信息
				this.getEmailModuleByModuleName(request, response);
				break;
			case 1:
				//添加或者更新邮件模板
				this.addOrUpdateEmailModule(request, response);
				break;
		}
	}
	
	/**
	 * 根据邮件模板名称获取模板信息
	 * @param request
	 * @param response
	 */
	private void getEmailModuleByModuleName(HttpServletRequest request, HttpServletResponse response){
		String moduleName = request.getParameter("moduleName");
		BEmailModule handler = new BEmailModule();
		try {
			MEmailModule moduleInfo = handler.getEmailModuleByModuleName(moduleName);
			if(moduleInfo == null){
				super.response(response, "");
			}else{
				Gson gson = new Gson();
				super.response(response, gson.toJson(moduleInfo));
			}
		} catch (Exception e) {
			e.printStackTrace();
			MLLogFactory.exceptionLog("根据邮件模板名称获取模板信息出错", "根据邮件模板名称获取模板信息出错", e.getMessage(), request.getRemoteAddr());
			super.response(response, "");
		}
	}

	/**
	 * 添加或者更新邮件模板
	 * @param request
	 * @param response
	 */
	private void addOrUpdateEmailModule(HttpServletRequest request, HttpServletResponse response){
		try {
			MEmailModule moduleInfo = (MEmailModule)JsonHelper.getObjectByJsonStringAndGson(request, MEmailModule.class);
			BEmailModule handler = new BEmailModule();
			MEmailModule updateInfo = handler.getEmailModuleByModuleName(moduleInfo.getModuleName());
			if(updateInfo != null){
				//更新邮件模板
				updateInfo.setModuleContent(moduleInfo.getModuleContent());
				if(handler.updateEmailModuleByModuleName(updateInfo)> 0){
					super.response(response, "1");
					return;
				}else{
					super.response(response, "2");
					return;
				}
				
				
			}else{
				moduleInfo.setAddTime(DateUtil.getNowDate());
				//添加邮件模板
				if(handler.addEmailModule(moduleInfo)> 0){
					super.response(response, "3");
					return;
				}else{
					super.response(response, "4");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			MLLogFactory.exceptionLog("添加或者更新邮件模板出错", "添加或者更新邮件模板出错", e.getMessage(), request.getRemoteAddr());
			super.response(response, "0");
		}
		
		

	}
}
